package com.example.sls.dao.user;

import com.example.sls.pojo.user.Users;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 用户表(Users)表数据库访问层
 *
 * @author makejava
 * @since 2024-06-13 12:22:54
 */
public interface UsersDao {
  /**
   * 通过ID查询单条数据
   *
   * @param id 主键
   * @return 实例对象
   */
  @Select("select * from users where id=#{id}")
  Users queryById(Integer id);

  /**
   * 查询指定行数据
   *
   * @param users    查询条件
   * @param pageSize 分页对象
   * @param offset   偏移量，起始处
   * @return 对象列表
   */
  @Select("select * from users where 1=1" +
          " and (id=#{users.id} or (isnull(#{users.id}) or #{users.id}=0))" +
          " and (username=#{users.username} or (isnull(#{users.username}) or #{users.username}=''))" +
          " and (password=#{users.password} or (isnull(#{users.password}) or #{users.password}=''))" +
          " and (avatar=#{users.avatar} or (isnull(#{users.avatar}) or #{users.avatar}=''))" +
          " and (nickname=#{users.nickname} or (isnull(#{users.nickname}) or #{users.nickname}=''))" +
          " and (phone=#{users.phone} or (isnull(#{users.phone}) or #{users.phone}=''))" +
          " and (email=#{users.email} or (isnull(#{users.email}) or #{users.email}=''))" +
          " and (created_at=#{users.createdAt} or (isnull(#{users.createdAt}) or #{users.createdAt}=''))" +
          " limit #{pageSize} offset #{offset}")
  List<Users> queryAllByLimit(Users users, Integer pageSize, Integer offset);

  /**
   * 统计总行数
   *
   * @return 总行数
   */
  @Select("select count(*) from users")
  long count();

  /**
   * 新增数据
   *
   * @param users 实例对象
   * @return 影响行数
   */
  @Insert("insert into users (id,username,password,avatar,nickname,phone,email,created_at) " +
          "values(" +
          "if(#{id}!=0 and !isnull(#{id}),#{id},null)," +
          "if(#{username}!='' and !isnull(#{username}),#{username},null)," +
          "if(#{password}!='' and !isnull(#{password}),#{password},null)," +
          "if(#{avatar}!='' and !isnull(#{avatar}),#{avatar},null)," +
          "if(#{nickname}!='' and !isnull(#{nickname}),#{nickname},null)," +
          "if(#{phone}!='' and !isnull(#{phone}),#{phone},null)," +
          "if(#{email}!='' and !isnull(#{email}),#{email},null)," +
          "if(#{createdAt}!='' and !isnull(#{createdAt}),#{createdAt},null)" +
          ")")
  int insert(Users users);

  /**
   * 通过id修改数据
   * id 主键
   *
   * @param users 实例对象
   * @return 影响行数
   */
  @Update("update users set " +
          "username=if(isnull(#{username}) or #{username}='',username,#{username})," +
          "password=if(isnull(#{password}) or #{password}='',password,#{password})," +
          "avatar=if(isnull(#{avatar}) or #{avatar}='',avatar,#{avatar})," +
          "nickname=if(isnull(#{nickname}) or #{nickname}='',nickname,#{nickname})," +
          "phone=if(isnull(#{phone}) or #{phone}='',phone,#{phone})," +
          "email=if(isnull(#{email}) or #{email}='',email,#{email})," +
          "created_at=if(isnull(#{createdAt}) or #{createdAt}='',created_at,#{createdAt})" +
          " where id=#{id}")
  int update(Users users);

  /**
   * 通过主键删除数据
   *
   * @param id 主键
   * @return 影响行数
   */
  @Delete("delete from users where id=#{id}")
  int deleteById(Integer id);

  /**
   * 通过用户名查找
   *
   * @param username 用户名
   * @return 影响行数
   */
  @Select("select * from users where username=#{username}")
  Users findByUsersName(String username);
}

